home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Applications / Calculators / NumberCrunch 1.41 / Number Crunch II Demo / Library Routines / Text Examples / Discrete Calculus < prev    next >
Encoding:
Text File  |  1992-09-03  |  7.1 KB  |  200 lines  |  [TEXT/NCII]

  1. ################
  2. # Discrete Calculus
  3. #
  4. #   function DifferentiateArray(y,x)  # Returns array dy/dx[1…n] at x[1…n].
  5. #   function IntegrateArray(y,x)  # Returns array of indefinite integral of y[1…n].
  6. #   function Integrate(f,a,b) # Returns definite integral of f(x) dx from x=a to b.
  7. # Also see the routines in the Miscellaneous Routines file, which do some
  8. #  of the same things without using xCOD's.
  9. #
  10. #  This text file explains and gives examples of
  11. #  some of the routines in the file All Library Routines, which
  12. #  should be Opened before trying any of these examples.
  13. #################
  14.  
  15.  
  16. ###########
  17. # These are the xCODs, which are in the file 
  18. #
  19.  
  20. xDIFFERENTIATE_ARRAY
  21.   # xCOD xDIFFERENTIATE_ARRAY(N:num; X,Y, dy2, yDiff:RealArray), finds the derivative yDiff[1…N] of y[1…n] at x[1…n]. dy2[1…n] returns with the 2nd derivatives of y(x), used in the cubic spline fit; an external program.
  22.  
  23. xINTEGRATE_ARRAY
  24.   # xCOD xINTEGRATE_ARRAY(N:num; X,Y, dy2,Yintegral:RealArray), finds the indefinite integral yIntegral[1…N] of y[1…n] at x[1…n]. dy2[1…n] returns with the 2nd derivatives of y(x), used in the cubic spline fit; an external program.
  25.  
  26. xINTEGRATE
  27.   # xCOD xINTEGRATE(Prog SumF(n:num; zIO:array); a,b,Tol,Ans,Err:Num), finds Ans= definite integral of f(x) from a to b within Tol. SumF(4,zIO=[S,x1,x2,dx]) must calculate S=f(x1)+f(x1+dx)+…+f(x2)). Err: -1=> did not converge, lower Tol; an external program
  28.  
  29.  
  30. #############
  31. # Here are the interface routines.
  32.  
  33. # Given arrays x[1…n], y[1…n] with y=f(x),
  34. # this returns an array of the first derivatives of y at x.
  35. # xDIFFERENTIATE_ARRAY, like xINTEGRATE_ARRAY,
  36. # uses a cubic spline fit to y(x), which calculates the
  37. # 2nd derivatives dy2[1…n] of y(x).
  38. #
  39.   function DifferentiateArray(y,x)  # Returns array dy/dx[1…n] at x[1…n].
  40. .   var n, dy2, ans
  41. . #     Input:
  42. . #                     x, y = arrays
  43. . #     Output:
  44. . #                     DifferentiateArray = dy/dx array
  45. .   begin
  46. .     dy2=y; ans=y  # reserve storage space.
  47. .     n = size(y)
  48. .     xDIFFERENTIATE_ARRAY(n,x,y,dy2,ans)
  49. .     DifferentiateArray = ans
  50. .   end
  51. .   
  52.  
  53.  
  54. #   This is the inverse of DifferentiateArray. 
  55. #  Given x[1…n], y[1…n] with y=f(x), 
  56. #  IntegrateArray returns an array for the indefinite 
  57. #  integral of y from x[1] to x[j] at the points x[j].
  58. #    The first value of the returned result is
  59. #  always zero, since it is the integral from x[1] to x[1].
  60. #
  61.   function IntegrateArray(y,x)  # Returns array of indefinite integral of y[1…n].
  62. .   var n,dy2,ans
  63. .  #   Input:
  64. .  #             x,y = arrays
  65. .  #   Output:
  66. .  #             IntegrateArray = Integral from x[1] to x of y,  an array
  67. .  #
  68. .   begin
  69. .     dy2=y; ans=y # reserve storage space.
  70. .     n = size(y)
  71. .     xINTEGRATE_ARRAY(n,x,y,dy2,ans)
  72. .     IntegrateArray = ans
  73. .   end
  74. .  
  75.  
  76.  
  77. # Integrate returns the definite integral of 
  78. # an analytic function f(x) from x=a to x=b.
  79. # For this interface routine, 
  80. # f(x) should be an NCII function defined via a line like
  81. #    f(x) = x^2+3
  82. # or whatever.  
  83. # (You can pass another xCOD for SumF, as 
  84. #   long as it takes the two arguments zN,zIO=[S,x0,x1,dx]
  85. #   and calculates S=f(x0)+f(x0+dx)+…+f(x1).  )
  86. #
  87. # Set "Tol" to the desired accuracy of the result.
  88. #
  89.   function Integrate(f,a,b) # Returns definite integral of f(x) dx from x=a to b.
  90. .   var tol, ans, err
  91. . #   Input:
  92. . #             NCII user function f
  93. . #             a, b = numbers for left, right of integration range
  94. . #   Output:
  95. . #             Integrate = integral of f from a to b, a number.
  96. .   begin    # first, rename the function f(x).
  97. .     zzF_Integral__ = f
  98. .     tol = 1e-8  # going too high will demand very large arrays in SumF
  99. .     xINTEGRATE(zzFuncForIntegrate__, a, b, tol, ans, err)
  100. .     if err<>0 then 
  101. .        begin
  102. .          beep;
  103. .          Print(" Integrate did not converge ");
  104. .        end
  105. .     Integrate = ans
  106. .   end
  107. .   
  108.  
  109. # This is used by Integrate to evaluate
  110. # the function that is to be integrated.
  111. # The xCOD xINTEGRATE calls this
  112. # several times with different grid sizes, 
  113. # converging on the final value of the integral.
  114. #   zIO[1…4] is both input and output.
  115. # Input:
  116. #     zIO[2] = x1
  117. #     zIO[3] = x2
  118. #     zIO[3] = dx 
  119. #  Output:
  120. #     zIO[1] = sum(  f(x1)+f(x1+dx)+...+f(x2) )
  121. #  where f is the function zzF_Integral__ , 
  122. #  which is set from the passed function in Integrate, above.
  123. #
  124.  program zzFuncForIntegrate__(n4, zIO) # used with function Integrate(f,a,b)
  125. .   var  x
  126. .   begin
  127. .     x = zIO(2)…zIO(3)@zIO(4)
  128. .     zIO(1) = sum(zzF_Integral__(x))
  129. .   end
  130. .   
  131.  
  132.  
  133. ###########
  134. # Examples:     #
  135. ###########
  136.  
  137. ####
  138. # Differentiation and Integration of arrays.
  139. #
  140. # First, define the independent array x:
  141.  x = 0…π@.02;
  142. # Then the function y, and the exact derivative and integral.
  143.  y= sin(x); 
  144. ExactDeriv=cos(x);
  145. ExactIntegral = 1-cos(x);  # The 1 is the integration constant,
  146.                                          # chosen so that ExactIntegral[1]=0.
  147. # Then the approximate derivative and integral, using the
  148. # xCOD routines which fit cubic splines to the arrays :
  149. approxDeriv = DifferentiateArray(y,x);
  150. approxIntegral = IntegrateArray(y,x);
  151. #
  152. #     And a table of some of the results. To generate the full table,
  153. #     evaluate the "Table" line below, without the "#" at the front. Most of the
  154. #     entries have been deleted by hand, for brevity.
  155. #  Table(x,y,ExactDeriv,approxDeriv,ExactIntegral,approxIntegral)
  156. #
  157. # x                  y                  ExactDeriv   approxDeriv ExactIntegral  approxIntegral
  158. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  159.    0                  0                  1                  1                  0                  0
  160.    0.02             0.02             0.9998         0.9998          1.9999e-4    2e-4
  161.    0.04             0.03999       0.9992         0.9992          7.9989e-4    7.9992e-4
  162.    0.06             0.05996       0.9982         0.9982         0.0018         0.0018
  163. # ...
  164.    0.6               0.56464       0.82534       0.82534       0.17466       0.17467
  165.    0.62             0.58104       0.81388       0.81388       0.18612       0.18613
  166. # …
  167.    1.22             0.9391         0.34365       0.34365       0.65635       0.65638
  168.    1.24             0.94578       0.3248         0.3248         0.6752         0.67523
  169. # …
  170.    2.18             0.8201         -0.57221     -0.57221      1.57221       1.57227
  171.    2.2               0.8085         -0.5885       -0.5885        1.5885         1.58855
  172. # …
  173.    3.1               0.04158       -0.99914     -0.99914      1.99914       1.9992
  174.    3.12             0.02159       -0.99977     -0.99977      1.99977       1.99983
  175.    3.14             0.00159       -1                -1                 2                  2.00007
  176. #
  177. # The approximate results are very close to the exact answers. 
  178. # The maximum errors are
  179. maximum( | ExactDeriv - approxDeriv | )
  180.    1.129388e-9 
  181. maximum( | ExactIntegral - approxIntegral | )
  182.    6.666618e-5 
  183. # The second is larger because the errors accumulate during the intergration.
  184.  
  185.  
  186.  
  187. ############
  188. # Integration of an analytic function:
  189. #
  190. # Suppose we want to integrate f(x)=x+1 from x=0 to x=1. 
  191. # The exact answer is 1.5. 
  192. #
  193. f(x) = x+1;  Integrate(f, 0, 1)
  194.   1.5      # The numerical answer - right on the nose.
  195. #
  196.  
  197.  
  198.